package com.zrspring.libv2.base;

/*   
 * Copyright (c) 2014-2014 China.ChongQing.MaiQuan Ltd All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * Founder. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Founder.   
 *   
 */
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import butterknife.ButterKnife;

/**
* @ClassName: BaseFragment
* @Description: 基础的Fragment
* @author zrspring
* @date 2015-11-24 下午3:52:30
* @ConputerUserName zrspring 
*/

public abstract class BaseFragmentV4 extends Fragment {
	
	/**
	* @Variables savedBudleData : 用于保存数据的bundle
	*/
	public Bundle savedBudleData = null;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		bindingListener(activity);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Bundle info = getArguments();
		findBundleData(info);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		View fragmentView = inflater.inflate(setFragmentViewLayout(), container,false);
		ButterKnife.bind(this,fragmentView);
		iniView(fragmentView);
		if(savedInstanceState != null && savedBudleData == null) {
			savedBudleData = savedInstanceState.getBundle(getSaveDataBundleKeyName());
        }
        if(savedBudleData != null) {
        	readDataFromBundle(savedBudleData);
        }
		LoadData();
		savedBudleData = null;//完成以后重置用于保存数据的bundle 对象
		return fragmentView;
	}
	
	/**
	* @Title: getSaveDataBundleKeyName
	* @Description: 给储存数据的Bundle的Key名  
	* @return 
	* @throws
	*/
	public abstract String getSaveDataBundleKeyName();
	
	/**
	* @Title: resetDataToView
	* @Description: 如果View被从新加载，在那么就从savedInstanceState中取出指定的 bundle对象所保数据的，交给具体的实现类处理,需要检查是否为空
	* @throws
	*/
	public abstract void readDataFromBundle(Bundle dataBundle);
	
	/**
	* @Title: needSaveData
	* @Description: 将需要保存的数据，在此方法中去实现 
	* @return 返回一个已经储存数据的Bundle
	* @throws
	*/
	public abstract Bundle needSaveData();
	
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
        outState.putBundle(getSaveDataBundleKeyName(), (savedBudleData != null) ? savedBudleData : needSaveData());
	}
	
	@Override
    public void onDestroyView() {
        super.onDestroyView();
        savedBudleData = needSaveData();
    }
	
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		ButterKnife.unbind(this);
		cleanMemory();
	}
	
	/**
	* @Title: bindingListener
	* @Description: 绑定和Activity的接口 
	* @param activity
	* @return 
	* @throws
	*/
	public abstract int bindingListener(Activity activity);
	
	/**
	* @Title: setFragmentView
	* @Description: 设置布局   
	* @return 
	* @throws
	*/
	public abstract int setFragmentViewLayout();
	
	/**
	* @Title: iniView
	* @Description: 初始化布局   
	* @param fragmentView 
	* @throws
	*/
	public abstract void iniView(View fragmentView);
	
	/**
	* @Title: findBundleData
	* @Description: 获取activity传过来的数据
	* @param info 
	* @throws
	*/
	public abstract void findBundleData(Bundle info);

	/**
	* @Title: LoadData
	* @Description: 加载数据，本地，或者服务器
	* @throws
	*/
	public abstract void LoadData();
	
	/**
	* @Title: cleanMemory
	* @Description: 清空内存    
	* @throws
	*/
	public abstract void cleanMemory();
	
	/**
	* @Title: showToast
	* @Description: 弹出消息
	* @param msg 
	* @throws
	*/
	public void showToast(String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
	}

	/**
	* @Title: showToastInDevelopment
	* @Description: 显示在开发中
	* @throws
	*/
	public void showToastIsDevelopmenting() {
		Toast.makeText(getActivity(), "功能正在开发中", Toast.LENGTH_SHORT).show();
	}
}
